package com.station.Activitys.fragment;


import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.station.Activitys.R;
import com.station.Activitys.activity.ChangeActivity;
import com.station.Activitys.activity.MyTestMsgActivity;
import com.station.Activitys.activity.PlanActivity;
import com.station.Activitys.activity.StudyActivity;
import com.station.Activitys.activity.SurveyActivity;
import com.station.Activitys.activity.TestMsgActivity;
import com.station.Activitys.activity.WebDateActivity;
import com.station.Activitys.bean.ImageText;
import com.station.Activitys.bean.LatestNews;
import com.station.Activitys.bean.News;
import com.station.Activitys.fragment.base.BaseFragment;
import com.station.Activitys.http.manager.HomeManager;
import com.station.Activitys.utils.CacheManger;
import com.station.Activitys.utils.Constant;
import com.station.Activitys.utils.GlideUtils;
import com.station.Activitys.utils.JumperUtils;
import com.zhy.autolayout.AutoLinearLayout;

import org.simple.eventbus.Subscriber;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import cn.bingoogolapple.bgabanner.BGABanner;

/**
 * 项目名称：基站 手机app
 * 类描述：首页 fragment
 * 创建人：张飞祥
 * 创建时间：2018/7/19 9:18
 * 修改人：张飞祥
 * 修改时间：2018/7/19 9:18
 * 修改备注：
 */
public class HomeFragment extends BaseFragment {

    //标题
    @Bind(R.id.tv_title)
    TextView tvTitle;
    //轮播图
    @Bind(R.id.banner_home_icon)
    BGABanner bannerHomeIcon;
    //最新消息
    @Bind(R.id.tv_new_news)
    TextView tvNewNews;
    //设计院公司
    @Bind(R.id.layout_generation)
    AutoLinearLayout layoutGeneration;
    //代维公司
    @Bind(R.id.layout_design)
    AutoLinearLayout layoutDesign;

    //初始化网络请求
    public HomeManager mApiManager;

    @Override
    protected void initViews() {
        //网络请求接口 常驻方法
        mApiManager = mApiManager.getHomeApiManager(getActivity(), getActivity().getTaskId());
        //获取 用户 id
        mUser = CacheManger.getUser(mACache);

        //TODO 网络请求加载轮播图
        mApiManager.homeBanner();
        //TODO 网络请求加载最新消息
        mApiManager.homeLatestNews(mUser.getId());
        //设置标题
        tvTitle.setText("基站管理平台");

        //获取当前用户身份
        String type = mUser.getDepartmentType();

        if (type.equals(Constant.USER_UI))
            //显示设计院 公司的操作按钮
            layoutGeneration.setVisibility(View.VISIBLE);
        else
            //显示代维 公司的操作按钮
            layoutDesign.setVisibility(View.VISIBLE);

    }

    /**
     * 加载 轮播图 返回结果
     */
    @Subscriber(tag = Constant.BUS_HOME_BANNER)
    public void onBanner(List<ImageText> list) {
        //加载轮播图
        setBannerData(list);
    }

    /**
     * 设置 轮播图数据
     */
    private void setBannerData(final List<ImageText> list) {
        //设置 布局中 哪个控件加载某一个 图片
        bannerHomeIcon.setAdapter(new BGABanner.Adapter<ImageView, String>() {
            @Override
            public void fillBannerItem(BGABanner banner, ImageView itemView, String model, int position) {
                ImageView imageView = (ImageView) itemView.findViewById(R.id.iv_item_img);
                GlideUtils.loadBannerImage(getActivity(), model, imageView);
            }
        });

        //数据重装
        List<String> urls = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            urls.add(list.get(i).getImgUrl());
        }

        //设置数据源
        bannerHomeIcon.setData(R.layout.item_home_banner, urls, null);
        //设置轮播图响应事件
        bannerHomeIcon.setDelegate(new BGABanner.Delegate<ImageView, String>() {
            @Override
            public void onBannerItemClick(BGABanner banner, ImageView itemView, String model, int position) {
                /**
                 * 跳转 webView事件
                 */
                //TODO  传递 方法
                Bundle bundle = new Bundle();
                //标题
                bundle.putString(Constant.WEB_TITLE, "图文详情");
                //地址
                bundle.putString(Constant.WEB_URL, list.get(position).getActionUrl());
                //开始跳转
                JumperUtils.JumpTo(getActivity(), WebDateActivity.class, bundle);
            }
        });
    }

    /**
     * 加载 最新消息 返回结果
     */
    @Subscriber(tag = Constant.BUS_HOME_LATEST_NEWS)
    public void onLatestNews(LatestNews latest) {

        if ("1".equals(latest.getState())){

            //消息实体对象
            final News news = latest.getData();
            //设置标题
            tvNewNews.setText(news.getTitle());

            //TODO 按钮点击事件
            tvNewNews.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View view) {
                   //TODO 跳转 webView事件
                   Bundle bundle = new Bundle();
                   //标题
                   bundle.putString(Constant.WEB_TITLE, "信息详情");

                   //浏览地址
                   String newsUrl = Constant.HTTP_URL + "GraphicMessage/AppLoadDetialInfo?UserId=" +
                           mUser.getId() + "&Id=" + news.getId();
                   //地址
                   bundle.putString(Constant.WEB_URL, newsUrl);
                   //开始跳转
                   JumperUtils.JumpTo(getActivity(), WebDateActivity.class, bundle);
               }
           });
        }
    }

    /**
     * 按钮点击事件
     *
     * @return
     */
    @OnClick({R.id.layout_survey, R.id.layout_plan, R.id.layout_change, R.id.layout_study,
            R.id.layout_test_msg, R.id.layout_my_test_msg})
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.layout_survey:         //基站勘察
                JumperUtils.JumpTo(getActivity(), SurveyActivity.class);
                break;

            case R.id.layout_plan:           //站址规划
                JumperUtils.JumpTo(getActivity(), PlanActivity.class);
                break;

            case R.id.layout_study:          //可研信息库
                JumperUtils.JumpTo(getActivity(), StudyActivity.class);
                break;

            case R.id.layout_change:         //站址变更
                JumperUtils.JumpTo(getActivity(), ChangeActivity.class);
                break;

            case R.id.layout_test_msg:       //可验收信息
                JumperUtils.JumpTo(getActivity(), TestMsgActivity.class);
                break;

            case R.id.layout_my_test_msg:    //我的验收信息
                JumperUtils.JumpTo(getActivity(), MyTestMsgActivity.class);
                break;
        }
    }

    @Override
    protected int getContentViewId() {
        return R.layout.frg_home;
    }
}
